/**********************************************
*  Namespace: Phx
*  Classes:   CdnPhysicsManager
*  Desc:      Handles all physics in the game.
*             Singleton class.
*  Author:    Vaios Kalpias-Ilias (C) 2008-2009
***********************************************/

#include "CdnPhysicsManager.h"

namespace Phx
{	
	//----------------------------------------------------------
	CdnPhysicsManager* CdnPhysicsManager::m_pInstance = 0;
	//----------------------------------------------------------
	/**
	* Ctor
	*/
	CdnPhysicsManager::CdnPhysicsManager()
	{

	}
	//----------------------------------------------------------
	/**
	* Dtor
	*/
	CdnPhysicsManager::~CdnPhysicsManager()
	{

	}
	//----------------------------------------------------------
	/**
	* Returns single instance
	*/
	CdnPhysicsManager* CdnPhysicsManager::GetInstance()
	{
		return m_pInstance ? m_pInstance :
			m_pInstance = new CdnPhysicsManager();
	}
	//----------------------------------------------------------
	/**
	* Destroys single instance
	*/
	void CdnPhysicsManager::Destroy()
	{
		if (m_pInstance)
		{
			delete m_pInstance;
			m_pInstance = 0;
		}

	}
	//----------------------------------------------------------
	/**
	* Adds rigid body to the euler integration list
	*/
	void CdnPhysicsManager::RegisterRigidBodyEuler (const CdnRigidBody& rigidBody)
	{
		m_lEulerRigidBodies.push_back(rigidBody);
	}
	//----------------------------------------------------------
	/**
	* Adds rigid body to the RK4 integration list
	*/
	void CdnPhysicsManager::RegisterRigidBodyRk4 (const CdnRigidBody& rigidBody)
	{
		m_lRk4RigidBodies.push_back(rigidBody);
	}
	//----------------------------------------------------------
	/**
	* Performs physics step
	*/
	void CdnPhysicsManager::Update(float dt)
	{
		// integrate euler list
		IntegrateEulerList(dt);
		// integrate RK4 list
		IntegrateRk4List(dt);
	}
	//----------------------------------------------------------
	/**
	* Integrates Euler List
	*/
	void CdnPhysicsManager::IntegrateEulerList(float dt)
	{
		RigidBodyList::iterator it = m_lEulerRigidBodies.begin();
		for (; it != m_lEulerRigidBodies.end(); ++it)
		{
			CdnRigidBody &body = (*it);
			body.pos += body.vel * dt;
			body.vel += body.acc * dt;

		}
	}
	//----------------------------------------------------------
	/**
	* Integrates RK4 List
	*/
	void CdnPhysicsManager::IntegrateRk4List(float dt)
	{

	}

}
